﻿<div class="ld-tabs">
    <ul>
        <li class="k-state-active">网页列表</li>
        <li>内容</li>
    </ul>
    <div>
        <div class="k-block divToolbar">
            <div style="display: inline-block">
                <b>栏目：</b><select id="ddlCase" style="width: 260px"></select>
            </div>
            <div style="display: inline-block">
                <select id="ddlVisible" >
                    <option value="0">所有</option>
                    <option value="1">已审</option>
                    <option value="2">未审核</option>
                </select>
            </div>
            <button class="k-button" onclick="CreateNewPage()"><span class="k-icon k-i-plus"></span><span>添加新网页</span></button>
        </div>
        <div id="grid_pages" style="text-align: center"></div>
    </div>
    <div>
        <div id="pageEditor" style="height: 600px;"></div>
        <div style="padding:10px;">以下是英文内容</div>
        <div id="pageEditor_En" style="height: 600px;"></div>
        <div style="text-align: center; padding: 10px;">
            <button class="k-button" onclick="SavePageContent()"><span class="k-icon k-i-tick"></span><span>保存内容</span></button>
        </div>
    </div>
</div>

<script class="init">
    $("#ddlCase").kendoDropDownList({
        dataTextField: "CaseName",
        template: "[#=ParentCase.CaseName#] #=CaseName#",
        dataValueField: "ID",
        dataSource: Whale.dataSource({
            url: "/Admin/Page/GetCases",
            getParameters: function () {
                return {
                    onlySubCase: true,
                    getParent: true
                }
            }
        }),
        change: function (e) {
            Filters();
        }
    });
    $("#ddlVisible").kendoDropDownList({
        change: function (e) {
            Filters();
        }
    });
    $("#pageEditor").kendoEditor({});
    $("#pageEditor_En").kendoEditor({});
</script>

<script class="grid_page_columns">
    var pageFields = [{
        title: "显示",
        field: "Visible",
        template: "#=GetVisible(Visible)#",
        width: 50
    }, {
        title: "缩略图",
        field: "Thumbnail",
        template: "#=ShowPic(Thumbnail)#",
        attributes: { style: "text-align:center" },
        width: 160
    }, {
        title: "标题",
        field: "Title",
        width: 300
    }, {
        title: "英文标题",
        field: "ETitle",
        attributes: { style: "text-align:left" },
        width: 300
    }, {
        title: "日期",
        template: "#=kendo.toString(kendo.parseDate(CreatedOn),'yyyy-MM-dd')#",
        width: 100
    }, {
        title: "操作",
        template: "#=GetOperations(ID,Visible)#",
        attributes: { style: "text-align:left" }
    }];

    function GetVisible(v) {
        return v ? "是" : "否";
    }

    function ShowPic(img) {
        if (img == "" || img == null)
            img = "/content/images/nopic.jpg";
        var html = "<img src='" + img + "' style='width:141px; height:85px'/>";
        return html;
    }

    function GetOperations(id,visible) {
        var html = "<button class='k-button' onclick=\"Edit(" + id + ")\"><span class='k-icon k-i-pencil'></span><span>修改</span></button>";
        html += "<button class='k-button' onclick=\"Delete(" + id + ")\"><span class='k-icon k-i-close'></span><span>删除</span></button>";
        html += "<button class='k-button' onclick=\"ShowContent(" + id + ")\"><span class='k-icon k-i-search'></span><span>显示内容</span></button>";
        if (!visible) {
            @if (Phatent.Website.WebContext.CurrentContext.User.Identity.Name=="admin"){
                <text>html += "<button class='k-button' onclick=\"SetVisible(" + id + ")\"><span class='k-icon k-i-tick'></span><span>审核</span></button>";</text>
            }
        }
        return html;
    }

    function Edit(id) {
        if (wndGeneralEdit != null && typeof wndGeneralEdit != "undefined") {
            wndGeneralEdit.setOptions({ title: "修改网页" });
            wndGeneralEdit.refresh({ url: "/Admin/Page/Edit/" + id });
            wndGeneralEdit.open();
        }
    }

    function Delete(id) {
        if (confirm("真的要删除这个网页吗?")) {
            Whale.Remote.Operation({
                url: "/Admin/Page/DeletePage",
                parameters: { pageID: id },
                success: function (result) {
                    if ($("#grid_pages").length > 0)
                        $("#grid_pages").data("kendoGrid").dataSource.read();
                },
                error: function (msg) {
                    wndGeneralInformation.content(msg).open();
                }
            });
        }
    }

    function SetVisible(id) {
        Whale.Remote.Operation({
            url: "/Admin/Page/SetVisible",
            parameters: { pageID: id },
            success: function (result) {
                if ($("#grid_pages").length > 0)
                    $("#grid_pages").data("kendoGrid").dataSource.read();
            },
            error: function (msg) {
                wndGeneralInformation.content(msg).open();
            }
        });
    }

    function ShowContent(id) {
        pageID = id;
        $(".ld-tabs").data("kendoTabStrip").select(1);
        var lst = $("#grid_pages").data("kendoGrid").dataSource.data();
        $.each(lst, function (i, o) {
            if (o.ID == id) {
                $("#pageEditor").data("kendoEditor").value(o.Description);
                $("#pageEditor_En").data("kendoEditor").value(o.EDescription);
                return;
            }
        });
    }
</script>

<script class="grid_config">
    $(function () {
        $("#grid_pages").kendoGrid({
            columns: pageFields,
            dataSource: Whale.dataSource({
                url: "/Admin/Page/GetPages",
                getParameters: function () {
                    var temp = {
                        caseID: $("#ddlCase").data("kendoDropDownList").value()
                    }
                    var v = $("#ddlVisible").data("kendoDropDownList").value();
                    if (v == 1) {
                        temp = $.extend(temp, {visible:true});
                    }else if (v==2){
                        temp = $.extend(temp, {visible:false});
                    }
                    return temp;
                },
                pageSize: 10

            }),
            pageable: {
                messages: {
                    empty: "暂无数据"
                }
            },
            dataBound: function (e) {
                //让表头居中
                $("th.k-header").attr("style", "text-align:center; font-weight:bold;");
            }
        });
    });

    function CreateNewPage() {
        wndGeneralEdit.setOptions({ title: "添加新的网页" });
        wndGeneralEdit.refresh({ url: "/Admin/Page/Edit" });
        wndGeneralEdit.open();
    }

    function Filters() {
        $("#grid_pages").data("kendoGrid").dataSource.page(0);
    }

    var pageID = 0;
    function SavePageContent() {
        Whale.Remote.Operation({
            url: "/Admin/Page/SavePageContent",
            parameters: {
                pageID: pageID,
                description: $("#pageEditor").data("kendoEditor").value(),
                edescription: $("#pageEditor_En").data("kendoEditor").value()
            },
            success: function (result) {
                $("#grid_pages").data("kendoGrid").dataSource.read();
                wndGeneralInformation.content("保存成功").open();
            },
            error: function (msg) {
                wndGeneralInformation.content(msg).open();
            }
        });
    }
</script>
